clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use if LTG<. using $Work/ibes_ret, clear

gen STG2=medest24/medest12 -1 if medest12>0
gstats winsor STG2 LTG, cuts(1 99) by(datem) replace

tempfile LTG_STG
save "'LTG_STG'", replace

bys datem: egen Tot=total(mcap)
gen vw=LTG*(mcap/Tot)

drop Tot
bys datem: egen Tot=total(mcap * !missing(STG2))
gen vwSTG=STG2*(mcap/Tot)

collapse (mean) LTGew=LTG (sum) LTG=vw STG2=vwSTG, by(datem)
replace LTG=. if LTG==0

tsset datem
gen LMLTGagg=L1.LTG
gen LQLTGagg=L3.LTG
gen L1LTGagg=L12.LTG
gen L3LTGagg=L36.LTG
gen L5LTGagg=L60.LTG

gen LMSTGagg=L1.STG2
gen LQSTGagg=L3.STG2
gen L1STGagg=L12.STG2
gen L3STGagg=L36.STG2
gen L5STGagg=L60.STG2

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTGagg=F`j'.LTG-LTG
	gen d`x'LLTGagg=LTG-L`j'.LTG
}
gen dMoLLTGagg=LTG -L1.LTG
gen dQrLLTGagg=LTG -L3.LTG
keep datem *agg

tempfile LTGagg
save "`LTGagg'", replace
* ************************************************
use permco permno datem permco permno datem LTG STG2 using "'LTG_STG'"

tsset permno datem
gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen L1LTG=L12.LTG
gen L3LTG=L36.LTG
gen L5LTG=L60.LTG

gen LMSTG=L1.STG2
gen LQSTG=L3.STG2
gen L1STG=L12.STG2
gen L3STG=L36.STG2
gen L5STG=L60.STG2

gen dMoLLTG=LTG -L1.LTG
gen dQrLLTG=LTG -L3.LTG

gen dMoLSTG=STG2-L1.STG2
gen dQrLSTG=STG2-L3.STG2

tsset permno datem
forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'LLTG=LTG -L`j'.LTG
	gen d`x'LSTG=STG2-L`j'.STG2

}
* ************************************************
merge m:1 datem using "`LTGagg'"
keep permco permno datem dMoLLTG dMoLSTG LMLTG LMSTG dMoLLTGagg LMLTGagg dQrLLTG dQrLSTG LQLTG LQSTG dQrLLTGagg LQLTGagg d*LLTG d*LSTG L*LTG L*STG d*LLTGagg L*LTGagg  

merge 1:1 permco permno datem using $Work/AppendixB10_June, keepusing(permco permno datem L1FE1 L3FE3 L5FE5 cumQNRet* cumRetNew* dp)
keep if _merge==3
gen lndp=ln(dp)
drop _merge dp
keep if cumQNRetMo1<.
gstats winsor          lndp dMoLLTG dMoLSTG LMLTG LMSTG dMoLLTGagg LMLTGagg dQrLLTG dQrLSTG LQLTG LQSTG dQrLLTGagg LQLTGagg d*LLTG d*LSTG L*LTG L*STG L*FE* d*LLTGagg L*LTGagg, cuts(1 99) by(datem) replace
foreach var of varlist lndp dMoLLTG dMoLSTG LMLTG LMSTG dMoLLTGagg LMLTGagg dQrLLTG dQrLSTG LQLTG LQSTG dQrLLTGagg LQLTGagg d*LLTG d*LSTG L*LTG L*STG L*FE* d*LLTGagg L*LTGagg  {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
* **************************
keep if lndp<.
bys permco: gen count=_N
drop if count==1
drop count
* **************************
tsset permno datem
generate DLT=dMoLLTG
generate DST=dMoLSTG
generate LLT=LMLTG
generate LST=LMSTG
generate LFE=1
generate DLTa=dMoLLTGagg
generate LLTa=LMLTGagg

eststo fit1:	quietly: ivreghdfe 	cumQNRetMo1  lndp DLT LLT DST LST DLTa LLTa	, dkraay(1) a(permco) 
		quietly: areg		cumQNRetMo1  lndp DLT LLT DST LST DLTa LLTa	, a(permco) 	 
		predict  hat if e(sample)
eststo Uni1:	quietly: ivreghdfe 	cumRetNewMo1 lndp 		 if hat<.	, dkraay(1)  a(permco)
eststo All1:	quietly: ivreghdfe 	cumRetNewMo1 lndp   hat		 		, dkraay(1)  a(permco)
drop hat
* ******************
replace DLT=dQrLLTG
replace DST=dQrLSTG
replace LLT=LQLTG
replace LST=LQSTG
replace LFE=1
replace DLTa=dQrLLTGagg
replace LLTa=LQLTGagg

eststo fit3:  	quietly: ivreghdfe 	cumQNRetQr1  lndp DLT LLT DST LST DLTa LLTa 	, dkraay(4)  a(permco)
		quietly: areg	 	cumQNRetQr1  lndp DLT LLT DST LST DLTa LLTa 	, absorb(permco)  	 
		predict  hat if e(sample)
eststo Uni3:	quietly: ivreghdfe 	cumRetNewQr1 lndp 			if hat<., dkraay(4)  a(permco)
eststo All3:	quietly: ivreghdfe 	cumRetNewQr1 lndp 	hat			, dkraay(4)  a(permco)
drop hat
* ******************
replace DLT=d1LLTG
replace DST=d1LSTG
replace LLT=L1LTG
replace LST=L1STG
replace LFE=L1FE1
replace DLTa=d1LLTGagg
replace LLTa=L1LTGagg

eststo fit12:	quietly: ivreghdfe 	cumQNRetYr1  lndp DLT LLT DST LST LFE DLTa LLTa , dkraay(13)  a(permco)
		quietly: areg	 	cumQNRetYr1  lndp DLT LLT DST LST LFE DLTa LLTa , a(permco)  	 
		predict hat if e(sample)
eststo Uni12:	quietly: ivreghdfe 	cumRetNewYr1 lndp 			if hat<., dkraay(13)  a(permco)
eststo All12:	quietly: ivreghdfe 	cumRetNewYr1 lndp 	hat			, dkraay(13)  a(permco)
drop hat
* ******************
replace DLT=d3LLTG
replace DST=d3LSTG
replace LLT=L3LTG
replace LST=L3STG
replace LFE=L3FE3
replace DLTa=d3LLTGagg
replace LLTa=L3LTGagg

eststo fit36:	quietly: ivreghdfe 	cumQNRetYr3  lndp DLT LLT DST LST LFE DLTa LLTa , dkraay(37)  a(permco)
		quietly: areg 		cumQNRetYr3  lndp DLT LLT DST LST LFE DLTa LLTa , a(permco)  	 
		predict hat if e(sample)
eststo Uni36:	quietly: ivreghdfe 	cumRetNewYr3 lndp 			if hat<., dkraay(37)  a(permco)
eststo All36:	quietly: ivreghdfe 	cumRetNewYr3 lndp	hat			, dkraay(37)  a(permco)
drop hat
* ******************
replace DLT=d5LLTG
replace DST=d5LSTG
replace LLT=L5LTG
replace LST=L5STG
replace LFE=L5FE5
replace DLTa=d5LLTGagg
replace LLTa=L5LTGagg

eststo fit60:	quietly: ivreghdfe 	cumQNRetYr5  lndp DLT LLT DST LST LFE DLTa LLTa , dkraay(61)  a(permco)
		quietly: areg	 	cumQNRetYr5  lndp DLT LLT DST LST LFE DLTa LLTa , a(permco)  	 
		predict hat if e(sample)
eststo Uni60:  	quietly: ivreghdfe 	cumRetNewYr5 lndp 			if hat<., dkraay(61)  a(permco)
eststo All60:  	quietly: ivreghdfe 	cumRetNewYr5 lndp 	hat			, dkraay(61)  a(permco)

drop hat
* ***************************
local file "$Tables/AppendixC5_June.csv"
esttab fit* 	 using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) order(lndp DLT LLT DST LST LFE DLTa LLTa) replace
esttab Uni* All* using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) order(lndp hat) append
* *****************************************************
